package com.yanqu.road.server.manger.activity.dafuweng.event;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWEvent;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.activity.dafuweng.building.BaseDFWBuilding;
import com.yanqu.road.server.manger.activity.dafuweng.entiety.CollectEventResult;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * @Author wpj
 * @Date 2021/1/7
 * 宝箱事件
 */
public class DFWBoxEvent extends BaseDFWEvent {

    public DFWBoxEvent(BaseDFWBuilding baseDFWBuilding, DFWEvent dfwEvent) {
        super(baseDFWBuilding, dfwEvent);
    }

    @Override
    public void doEvent() {
        DFWConfig config = baseDFWBuilding.getDfwConfig();
        DFWEvent dfwEvent = DaFuWengMgr.getDfwConfig().getDfwEventMap().get(this.dfwEvent.getEventId());

        // 清空下上次宝箱的冗余背包奖励
        userData.setBoxEventBagReward("");

        // 奖励的数量
//        int addition = DaFuWengMgr.getOpenBoxSkillAddition(config, baseDFWBuilding.getPlayer().getUserInfo().getUnionUid());
        int addition = 0; // 策划不要这个buff了
        int baseTimes = dfwEvent.getParamList().get(0);
        int rewardCount = addition + baseTimes;

        // 获取奖励和权重
        List<String> rewardWeightList = StringUtils.stringToStringList(dfwEvent.getRewards(), "\\|");
        List<String> rewardList = new ArrayList<>();
        List<Integer> weightList = new ArrayList<>();
        for (String rewardWeight : rewardWeightList) {
            List<String> rewardWeightSplit = StringUtils.stringToStringList(rewardWeight, ";");
            rewardList.add(rewardWeightSplit.get(0));
            weightList.add(Integer.valueOf(rewardWeightSplit.get(1)));
        }

        // 没商会无法获得收集类道具
        boolean haveUnion = !StringUtils.isNullOrEmpty(baseDFWBuilding.getUnionUid());

        // 收集类道具ID
        Set<Integer> collectIdSet = new HashSet<>();
        collectIdSet.add(eItemId.DFW_COLLECT_BU_PI.getValue());
        collectIdSet.add(eItemId.DFW_COLLECT_XIANG_LIAO.getValue());
        collectIdSet.add(eItemId.DFW_COLLECT_QIN_PU.getValue());
        collectIdSet.add(eItemId.DFW_COLLECT_TIE_QI.getValue());
        collectIdSet.add(eItemId.DFW_COLLECT_HUA_JUAN.getValue());

        // 随机抽奖励
        Property commonProp = new Property(); // 普通道具奖励
        Property collectProp = new Property(); // 收集事件的道具奖励（包括铁器）
        Random random = new Random();
        for (int i = 0; i < rewardCount; i++) {
            if (weightList.size() <= 0) {
                break;
            }
            int index = RandomHelper.getRandomIndexByWeight(weightList, random);
            Property tmpReward = PropertyHelper.parseStringToProperty(rewardList.get(index));
            boolean containCollectItem = containGoods(tmpReward, collectIdSet);
            if (containCollectItem && !haveUnion) {
                // 【收集类奖励没商会，重新抽】
                i--;
            } else if (containCollectItem && haveUnion) {
                // 【收集类奖励有商会，还要再判断】
                // 直接看能不能收集成功
                CollectEventResult eventResult = DaFuWengMgr.doCollectEvent(baseDFWBuilding.getUnionUid(), tmpReward, baseDFWBuilding.getPlayer().getUserId());
                if (!eventResult.isCanDoCollectEvent()) {
                    // 不能收集成功就跳过
                    i--;
                } else {
                    // 收集成功就加奖励
                    collectProp.addProperty(tmpReward);
                    if (eventResult.isUnionCollectReachReward()) {
                        boxCollectReward.addProperty(collectProp); // 收集类触发奖励，记录道具ID
                    }
                    if (eventResult.getNewBattleSkillId() != 0) {
                        //newBattleSkillId = eventResult.getNewBattleSkillId(); // 其利断金升级，记录最新值
                    }
                }
            } else {
                // 【其他普通道具，直接加】
                commonProp.addProperty(tmpReward);
            }
            rewardList.remove(index);
            weightList.remove(index);
        }

        // 设置加背包的奖励
        userData.setBoxEventBagReward(PropertyHelper.parsePropertyToString(commonProp));

        // 设置总奖励
        Property allReward = new Property();
        allReward.addProperty(commonProp);
        allReward.addProperty(collectProp);
        userData.setExpandParam(PropertyHelper.parsePropertyToString(allReward));
        reward = PropertyHelper.parsePropertyToString(allReward);

        // 如果是开金箱，统计下次数
        List<DFWEvent> events = config.getSkillMap().get(eDaFuWengEventType.OpenBoxSkill);
        if (events != null && !events.isEmpty() && events.get(0).getUpgradeParamList().get(3) == dfwEvent.getEventId()) {
            userData.setBoxOpenTimes(userData.getBoxOpenTimes() + 1);
            userData.setUpdateOption();
            if (!StringUtils.isNullOrEmpty(baseDFWBuilding.getUnionUid())) {
                DaFuWengMgr.notifyUnionData(baseDFWBuilding.getPlayer());
            }
        }

        // 奖励加背包
        baseDFWBuilding.getPlayer().getModule(CurrencyModule.class).addCurrency(PropertyHelper.parseStringToProperty(userData.getBoxEventBagReward()),
                eLogMoneyType.DaFuWeng, eLogMoneyType.DaFuWengBoxReward);
    }

    /**
     * 是否包含道具
     *
     * @param goodsIdSet 道具ID集合
     */
    public boolean containGoods(Property property, Set<Integer> goodsIdSet) {
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            if (goodsIdSet.contains(entry.getKey())) {
                return true;
            }
        }
        return false;
    }
}
